﻿using System;
using System.Collections.Generic;

namespace ICommonLibrary.Service.Diagnostics {

    public class TracingService : IService {

        public string Id { get; private set; }
        public string Name { get; set; }
        public ServiceState ServiceState { get; private set; }
        public List<Error> Errors { get; private set; }

        public TracingService() {
            Id = Guid.NewGuid().ToString();
            Name = GetType().Name + "#" + new object().GetHashCode();
            ServiceState = new ServiceState();
            Errors = new List<Error>();
        }

        public void Log(string serviceName, Exception e) {
            Errors.Add(new Error(serviceName, e));
            ServiceState.Message += serviceName + ":" + e.Message + ";";
        }

        public void Init() {
            ServiceState.Code = this.ServiceState.Initialized;
        }

        public void Start() {
            ServiceState.Code = this.ServiceState.Started;
        }

        public void Stop() {
            ServiceState.Code = this.ServiceState.Stopped;
        }

        public void Destroy() {
            ServiceState.Code = this.ServiceState.Destroyed;
        }

        public class Error {

            public string ServiceName { get; set; }

            public Exception Exception { get; set; }

            public Error(string serviceName, Exception exception) {
                ServiceName = serviceName;
                Exception = exception;
            }
        }

    }
}